﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using EbbinghausWord.BLL;
using EbbinghausWord.Entities;
using Microsoft.Phone.Controls;

namespace EbbinghausWord.Pages
{
    public partial class VoicePage : PhoneApplicationPage
    {
        public VoicePage()
        {
            InitializeComponent();

            List<BookVoiceEntity> bookVoices = null;
            loadingControl.Loading(
                () =>
                {
                    bookVoices = VoiceManager.GetAllBookVoiceInfo();
                },
                () =>
                {
                    voiceListBox.ItemsSource = bookVoices;
                });
        }


        #region private field
        /// <summary>
        /// 是否正在下载
        /// </summary>
        private bool _isDownload = false;
        private readonly VoiceManager _voiceManager = new VoiceManager();

        #endregion

        #region callback
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show(string.Format("您将要下载 {0} 的单词发音文件，建议开启Wi-Fi或连接电脑，你是否继续下载？",
                ((sender as Button).Tag as BookVoiceEntity).BookName), "下载单词发音", MessageBoxButton.OKCancel)
                         == MessageBoxResult.OK)
            {
                //修改界面
                downloadProgressPanel.Visibility = Visibility.Visible;

                //下载发音文件
                _voiceManager.MuliDownloadPercentaging +=
                    (s, ex) => Dispatcher.BeginInvoke(
                        () => progressTextBlock.Text = string.Format("{0}/{1}", ex.HaveDownloadCount, ex.TotalCount));
                _voiceManager.MultiDownloadVoiceCompleted += (s, ex) => Dispatcher.BeginInvoke(
                    () =>
                    {
                        //刷新界面
                        voiceListBox.ItemsSource = VoiceManager.GetAllBookVoiceInfo();

                        //提示信息
                        Tools.ShowMessage("下载完成");
                        downloadProgressPanel.Visibility = Visibility.Collapsed;

                        _isDownload = false;
                    });

                _voiceManager.StartDownloadVoice(((sender as Button).Tag as BookVoiceEntity).BookId);
                _isDownload = true;
            }
        }

        private void cancelClick(object sender, RoutedEventArgs e)
        {
            CancelDownload();
        }



        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (_isDownload)
            {
                CancelDownload();

                e.Cancel = true;
            }
            base.OnBackKeyPress(e);
        }

        #endregion

        #region private method
        private void CancelDownload()
        {
            //停止下载发音文件
            _voiceManager.StopDownloadVoice();
            //刷新界面
            downloadProgressPanel.Visibility = Visibility.Collapsed;
            //提示信息
            Tools.ShowMessage("您取消了下载");

            _isDownload = false;

            voiceListBox.ItemsSource = VoiceManager.GetAllBookVoiceInfo();
        }
        #endregion

    }
}